package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.domain.Candidate;
import com.ruoyi.system.domain.Client;
import com.ruoyi.system.domain.SysMail;
import com.ruoyi.system.service.ICandidateService;
import com.ruoyi.system.service.IClientService;
import com.ruoyi.system.service.ISysMailService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/system/statistics")
public class StatisticsController extends BaseController {

    @Resource
    private IClientService clientService;

    @Resource
    private ICandidateService candidateService;

    @Resource
    private ISysMailService sysMailService;

    @Resource
    private SysPermissionService permissionService;

    @GetMapping(value = "/ccsCount")
    public AjaxResult getCCSCount(){
        //查询当前用户角色
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //统计客户数
        Client client = new Client();
        Candidate candidate = new Candidate();
        SysMail sysMail = new SysMail();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        if (roles != null && roles.size() > 0) {
            if (roles.contains("admin") || roles.contains("boss")) {

            } else {
                client.setCreateUserId(getUsername());
                candidate.setCreateUserid(getUserId());
                sysMail.setUserId(getUserId());
            }
        } else {
            client.setCreateUserId(getUsername());
            candidate.setCreateUserid(getUserId());
            sysMail.setUserId(getUserId());
        }
        //客户数
        Long aLong = clientService.selectClientCount(client);
        Long candCount = candidateService.selectCandCount(candidate);
        Long mailCount = sysMailService.selectMailCount(sysMail);
        Map<String, Long> rel = new HashMap<>();
        if (aLong == null) {
            rel.put("clientCount", 0L);
        } else {
            rel.put("clientCount", aLong);
        }
        if (candCount == null) {
            rel.put("candCount", 0L);
        } else {
            rel.put("candCount", candCount);
        }
        if (mailCount == null) {
            rel.put("mailCount", 0L);
        } else {
            rel.put("mailCount", mailCount);
        }
        return AjaxResult.success(rel);
    }

}
